package com.wman.lib;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wman.sheep.common.utils.StatusBarUtil;
import com.wman.sheep.common.utils.ToastUtil;
import com.wman.sheep.mvp.base.BaseActivity;
import com.wman.lib.delegate.MainDelegate;
import com.wman.lib.fragment.TabCategoryFragment;
import com.wman.lib.fragment.TabHomeFragment;
import com.wman.lib.fragment.TabOrderFragment;
import com.wman.lib.fragment.TabShopCarFragment;

import java.util.Timer;
import java.util.TimerTask;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends BaseActivity<MainDelegate> {
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private int mCurrentTab;

    /**
     * 用于对Fragment进行管理
     */
    public FragmentManager fragmentManager;

    /**
     * 首页
     */
    private TabHomeFragment mTabHomeFragment;
    private ImageView mIvHome;
    private TextView mTvHome;
    /**
     * 分类
     */
    private TabCategoryFragment mTabCategoryFragment;
    private ImageView mIvExpand;
    private TextView mTvExpand;
    /**
     * 购物车
     */
    private TabShopCarFragment mTabShopCarFragment;
    private ImageView mIvCustomer;
    private TextView mTvCustomer;
    /**
     * 订单
     */
    private TabOrderFragment mTabOrderFragment;
    private ImageView mIvMy;
    private TextView mTvMy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fragmentManager = getSupportFragmentManager();
        //检查更新
        checkUpdate();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected boolean isShowTitleBar() {
        return false;
    }

    @Override
    protected Class getDelegateClass() {
        return MainDelegate.class;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initViewSC() {
        super.initViewSC();

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

        // 第一次启动时选中第0个tab
        getUiHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setTabSelection(0);
            }
        }, 1000);
    }

    @Override
    public void onNoDoubleClick(View v) {

    }

    @Override
    protected void bindEventListener() {
        mIvHome = mViewDelegate.get(R.id.iv_home);
        mTvHome = mViewDelegate.get(R.id.tv_home);
        mIvExpand = mViewDelegate.get(R.id.iv_expand);
        mTvExpand = mViewDelegate.get(R.id.tv_expand);
        mIvCustomer = mViewDelegate.get(R.id.iv_customer);
        mTvCustomer = mViewDelegate.get(R.id.tv_customer);
        mIvMy = mViewDelegate.get(R.id.iv_my);
        mTvMy = mViewDelegate.get(R.id.tv_my);

        mViewDelegate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabSelection(0);
            }
        }, R.id.ll_home);

        mViewDelegate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabSelection(1);
            }
        }, R.id.ll_expand);

        mViewDelegate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabSelection(2);
            }
        }, R.id.ll_customer);

        mViewDelegate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTabSelection(3);
            }
        }, R.id.ll_my);
    }

    /**
     * 检查更新
     */
    private void checkUpdate() {
        // TODO: 2019/08/18 0018
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mTabHomeFragment != null) {
            transaction.hide(mTabHomeFragment);
        }
        if (mTabCategoryFragment != null) {
            transaction.hide(mTabCategoryFragment);
        }
        if (mTabShopCarFragment != null) {
            transaction.hide(mTabShopCarFragment);
        }
        if (mTabOrderFragment != null) {
            transaction.hide(mTabOrderFragment);
        }
    }

    /**
     * 清除掉所有的选中状态
     */
    private void clearSelection() {
        mIvHome.setImageResource(R.mipmap.tab_icon_home_page);
        mIvExpand.setImageResource(R.mipmap.tab_icon_expand);
        mIvCustomer.setImageResource(R.mipmap.tab_icon_customer);
        mIvMy.setImageResource(R.mipmap.tab_icon_mine);

        mTvHome.setTextColor(getResources().getColor(R.color.main_tab_text_normal_color));
        mTvExpand.setTextColor(getResources().getColor(R.color.main_tab_text_normal_color));
        mTvCustomer.setTextColor(getResources().getColor(R.color.main_tab_text_normal_color));
        mTvMy.setTextColor(getResources().getColor(R.color.main_tab_text_normal_color));
    }

    /**
     * 根据传入的index参数来设置选中的tab页。
     */
    private void setTabSelection(int index) {
        clearSelection();
        // 开启一个Fragment事务
        if (!isFinishing()) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
            hideFragments(transaction);

            StatusBarUtil.setStatusBarColor(this, 0XFFFFFFFF);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

            switch (index) {
                case 0:
                    mIvHome.setImageResource(R.mipmap.tab_icon_home_page1);
                    mTvHome.setTextColor(getResources().getColor(R.color.main_tab_text_select_color));
                    if (mTabHomeFragment == null) {
                        mTabHomeFragment = new TabHomeFragment();
                        transaction.add(R.id.fragment_content, mTabHomeFragment);
                    } else {
                        transaction.show(mTabHomeFragment);
                    }
                    break;
                case 1:
                    mIvExpand.setImageResource(R.mipmap.tab_icon_expand1);
                    mTvExpand.setTextColor(getResources().getColor(R.color.main_tab_text_select_color));
                    if (mTabCategoryFragment == null) {
                        mTabCategoryFragment = new TabCategoryFragment();
                        transaction.add(R.id.fragment_content, mTabCategoryFragment);
                    } else {
                        transaction.show(mTabCategoryFragment);
                    }
                    break;
                case 2:
                    mIvCustomer.setImageResource(R.mipmap.tab_icon_customer1);
                    mTvCustomer.setTextColor(getResources().getColor(R.color.main_tab_text_select_color));
                    if (mTabShopCarFragment == null) {
                        mTabShopCarFragment = new TabShopCarFragment();
                        transaction.add(R.id.fragment_content, mTabShopCarFragment);
                    } else {
                        transaction.show(mTabShopCarFragment);
                    }

                    break;
                case 3:
                    mIvMy.setImageResource(R.mipmap.tab_icon_mine1);
                    mTvMy.setTextColor(getResources().getColor(R.color.main_tab_text_select_color));
                    if (mTabOrderFragment == null) {
                        // 如果MessageFragment为空，则创建一个并添加到界面上
                        mTabOrderFragment = new TabOrderFragment();
                        transaction.add(R.id.fragment_content, mTabOrderFragment);
                    } else {
                        transaction.show(mTabOrderFragment);
                    }
                    break;
            }
            mCurrentTab = index;
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); // 调用双击退出函数
        }
        return false;
    }

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            ToastUtil.showTextToast("别按啦,再按一次就要退出去了！");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            ToastUtil.cancel();
            getUiHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    AppContext.getActivityManager().exitApp(MainActivity.this);
                }
            }, 200);
        }
    }
}
